package com.hellord.ArmoryTools 
{
	import mx.utils.ObjectUtil;
	/**
	 * ...
	 * @author Matteo Cappelloni
	 */
	public dynamic class Item extends Object
	{
		public var itemName:String;
		public var itemId:uint;
		public var itemType:uint;
		public var itemSubType:String;
		public var durability:uint;
		public var ilvl:uint;
		public var reqlvl:uint;
		public var stack:uint;
		public var weapon:Object;
		public var stats:Object;
		public var spells:Array;
		
		public var is_heroic:Boolean = false;
		public var is_weapon:Boolean = false;
		
		private var _classes:Array;
		private var _icon:String;
		private var _sockets:Array;
		private var overallQualityId:uint;
		private var itemSource:XML;
		
		private const statNames:Array = new Array("Agility", "Intellect", "Spirit", "Stamina", "Strength");
		private const defensiveRatingNames:Array = new Array("DefenseSkillRating", "DodgeRating", "ManaRegen", "ParryRating", "ResilienceRating");
		private const offensiveRatingNames:Array = new Array("ArmorPenetration", "Attack Power", "CritRating", "ExpertiseRating", "HitRating", "HasteRating", "SpellPower");
		private const resistNames:Array = new Array("Arcane", "Fire", "Frost", "Holy", "Nature", "Shadow");
		private const spellRatingNames:Array = new Array("Crit", "Hit", "Haste", "SpellPower");
		private const initArray:Array = new Array().concat(statNames, defensiveRatingNames, offensiveRatingNames, resistNames);	
		
		private const itemTypes:Array = new Array("Consumable", "Bag", "Weapon","Gem","Armor","","Projectile","Trade Goods","Recipe","Enchant","Currency","","BoA Enchant","Key","","Misc","Glyph");
		public function Item(xml:XML) 
		{
			init();
			parseItem(new XML(xml.itemTooltip));
		}
		private function init():void
		{
			stats = new Object();
			weapon = new Object();
			
			/*
			for each (var i:String in initArray) {
				stats[i] = 0;		
			}*/
		}
				
		public function set classes(... cl):void { 
			_classes = new Array();
			for each (var i:String in cl) _classes.push(i);
		}
		public function get classes():Array {
			return _classes;
		}
		
		public function set sockets(o:Array):void {	_sockets = o; }
		public function get sockets():Array { return _sockets; }		
		
		public function toString():String
		{
			var st:String = "";
			for each (var i:String in initArray)
			{
				if(stats[i]) st += i + ": "+stats[i]+", ";
			}
			//trace("ST: " + st);
			return st;
		}
		
		private function parseItem(xml:XML):Item
		{
			itemName = xml.name;
			itemId = xml.id;
			overallQualityId = xml.overallQualityId;
			durability = xml.durability;
			reqlvl = xml.requiredLevel;
			ilvl = xml.itemLevel;
			_icon = xml.icon;
			stack = xml.maxCount
			itemType = xml.classId;
			//itemSource = xml.itemSource;
			
			if (xml.hasOwnProperty("heroic")) is_heroic = true;
			if (xml.hasOwnProperty("armor"))
			{
				var arm:uint = xml.armor;
				stats.armor = arm;
				stats.armorBonus = xml.armor.@armorBonus;
			}
			// set stats
			for each (var i:String in initArray)
			{
				if (xml.hasOwnProperty("bonus" + i))
				{
					var st:uint = xml["bonus" + i];
					stats[i] = st;
				}
			}
			// sets if a weapon
			if (xml.damageData.hasOwnProperty("damage"))
			{
				is_weapon = true;
				var dam:XML = new XML(xml.damageData.damage);
				
				var min:uint = dam.min;
				var max:uint = dam.max;
				var tp:uint = dam.type;
				var speed:Number = xml.damageData.speed;
				var dps:Number = xml.damageData.dps;
				
				weapon.min = min;
				weapon.max = max;
				weapon.type = tp;
				weapon.speed = speed;
				weapon.dps = dps;
				
			}  else is_weapon = false;
			//checks for sockets
			if (xml.hasOwnProperty("socketData"))
			{
				var soc:Array = new Array();
				for each (var sk:XML in xml.socketData.socket) soc.push(sk.@color);
				sockets = soc;
			}
			// check for class limitations
			if (xml.hasOwnProperty("allowableClasses"))
			{
				var arTemp:Array = new Array();
				//for each (var cl:String in xml.allowableClasses["class"]) arTemp.push(cl["class"].toString());
				classes = arTemp;
			}
			// check for spells
			if (xml.hasOwnProperty("spellData"))
			{
				spells = new Array();
				for each(var sp:XML in xml.spellData.spell)
				{
					var spo:Object = new Object();
					spo.desc = sp.desc.toString();
					spo.trigger = (sp.trigger)? true : false;
					spo.charges = parseInt(sp.maxCharges.toString());
					spells.push(spo);
				}
			}
			//trace(ObjectUtil.toString(this));
			//trace("***************************");
			//trace(xml);
			return this;
		}
	}

}